set scheme tufte
local us_color="blue"
local jp_color="orange_red"

global DATA  "path/to/input/data"   
global OUT "path/to/output/folder"
global INT "path/to/intermediate/files"
//=======================================
// FIGURE A1
//=======================================

//Unconditional set

use "$DATA\twincites_applicant_did_prep_citecounts.dta", clear
local policy_date=14943
local window_size=7
local window_num=26
gen time=floor((US_filing_date_d_cited-`policy_date')/`window_size') + 0.5
gen count=1
replace cite_count=ln(cite_count)
				
collapse (mean) cite_count (sum) count, by(nonJP time)

sum cite_count if nonJP==1 & time<0 [fweight=count]
gen US_mean=r(mean) if time<0
sum cite_count if nonJP==1 & time>=0 [fweight=count]
replace US_mean=r(mean) if time>=0
sum cite_count if nonJP==0 & time<0 [fweight=count]
gen JP_mean=r(mean) if time<0
sum cite_count if nonJP==0 & time>=0 [fweight=count]
replace JP_mean=r(mean) if time>=0

sort time nonJP
tw (sc cite_count time if nonJP==1 , color(`us_color') m(diamond_hollow) leg(lab(1 US inventors))) (sc cite_count time if nonJP==0, color(`jp_color') m(circle) leg(lab(2 JP inventors))) (line US_mean time, color(`us_color') lp(dash) leg(lab(3 US mean))) (line JP_mean time, color(`jp_color') lp(dash) leg(lab(4 JP mean))),  xline(0, lwidth(thin) lcolor(gray)) yti(log(citation count)) xti(US application date (weeks relative to AIPA implementation)) xlabel(-26 0 26) ylabel(0.5 0.75 1) leg(cols(2))
save "$OUT\citecounts_applicant_graph_uncond.dta", replace

graph export "$OUT\parallel_trend_applicant_citecounts_uncond.pdf", as(pdf) name("Graph") replace


//=======================================
// FIGURE A2
//=======================================

//Conditional set
use "$DATA\twincites_applicant_did_prep_citecounts.dta", clear
drop if cite_count==0
bysort family_cited: egen total=count(family_cited)
keep if total==2
local policy_date=14943
local window_size=7
local window_num=26
gen time=floor((US_filing_date_d_cited-`policy_date')/`window_size') + 0.5
gen count=1
replace cite_count=ln(cite_count)
				
collapse (mean) cite_count (sum) count, by(nonJP time)

sum cite_count if nonJP==1 & time<0 [fweight=count]
gen US_mean=r(mean) if time<0
sum cite_count if nonJP==1 & time>=0 [fweight=count]
replace US_mean=r(mean) if time>=0
sum cite_count if nonJP==0 & time<0 [fweight=count]
gen JP_mean=r(mean) if time<0
sum cite_count if nonJP==0 & time>=0 [fweight=count]
replace JP_mean=r(mean) if time>=0

sort time nonJP
tw (sc cite_count time if nonJP==1 , color(`us_color') m(diamond_hollow) leg(lab(1 US inventors))) (sc cite_count time if nonJP==0, color(`jp_color') m(circle) leg(lab(2 JP inventors))) (line US_mean time, color(`us_color') lp(dash) leg(lab(3 US mean))) (line JP_mean time, color(`jp_color') lp(dash) leg(lab(4 JP mean))),  xline(0, lwidth(thin) lcolor(gray)) yti(log(citation count)) xti(US application date (weeks relative to AIPA implementation)) xlabel(-26 0 26) ylabel(0.5 0.75 1) leg(cols(2))
save "$OUT\citecounts_applicant_graph_cond.dta", replace

graph export "$OUT\parallel_trend_applicant_citecounts_cond.pdf", as(pdf) name("Graph") replace
